package com.apes.hr.paySupport.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: SupportApplication
 * @Description:付费支援申请单
 * @author: ZhengZeXian
 * @create: 2021/4/15 10:07
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupportApplication.CONST_TABLE_NAME)
public class SupportApplication extends Model {

    public static final String CONST_TABLE_NAME = "hr_support_application";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付费支援申请单号", prefix = "S", padding = 10)
    @Comment("付费支援申请单号")
    private String id;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "delete", name = "删除"),
            @State(id = "transApprove", name = "转审核"),
            @State(id = "done", name = "审核"),
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 16, nullable = false)
    @Comment("单据状态")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("门店名称")
    private Dept dept;

    @Column(name = "candidates", length = 4000)
    @Comment("OA候选人")
    private String candidates;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    private PartyRole createUid; //创建人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @OneToMany(mappedBy = "supportApplication", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SupportApplicationDetail> supportApplicationDetails;

    public List<SupportApplicationDetail> getSupportApplicationDetails() {
        if (supportApplicationDetails == null) return null;
        for (SupportApplicationDetail detail : supportApplicationDetails) {
            detail.setSupportApplication(this);
        }
        return supportApplicationDetails;
    }

    @OneToMany(mappedBy = "supportApplication", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SupportApplicationItem> supportApplicationItems;

    public List<SupportApplicationItem> getSupportApplicationItems() {
        if (supportApplicationItems == null) supportApplicationItems = new ArrayList<>();
        for (SupportApplicationItem item : supportApplicationItems) {
            item.setSupportApplication(this);
        }
        return supportApplicationItems;
    }

    @Transient
    private List<SupportApplicationItem> transientItems;

    @Transient
    private String requestBranchName;

    @Transient
    private String requestPostName;

    @Transient
    private String supportDate;

    public String getRequestBranchName() {
        if (!supportApplicationDetails.isEmpty()){
            SupportApplicationDetail detail = supportApplicationDetails.get(0);
            return detail.getBranch() == null ? "" : detail.getBranch().getName();
        }
        return "";
    }

    public String getRequestPostName() {
        if (!supportApplicationDetails.isEmpty()){
            SupportApplicationDetail detail = supportApplicationDetails.get(0);
            return detail.getWorkPost() == null ? "" : detail.getWorkPost().getName();
        }
        return "";
    }

    public String getSupportDate() {
        if (!supportApplicationDetails.isEmpty()){
            SupportApplicationDetail detail = supportApplicationDetails.get(0);
            return DateUtil.format(detail.getStartDate(),DateUtil.SHOW_DATE_FORMAT) + detail.getStartTime().getDescription() + " — "
                    + DateUtil.format(detail.getEndDate(),DateUtil.SHOW_DATE_FORMAT) + detail.getEndTime().getDescription();
        }
        return "";
    }
}
